const cloneDeep = require('lodash/cloneDeep')
const { addTitle } = require('./handleData')

class Rxdb {
  static isBIM = key => {
    if (
      key === 'apartment' ||
      key === 'room' ||
      key === 'door' ||
      key === 'window' ||
      key === 'opening' ||
      key === 'scanstation' ||
      key === 'auto_plan_task' ||
      key === 'bim' ||
      key === 'mainwall' ||
      key === 'drywall' ||
      key === 'beam' ||
      key === 'wall' ||
      key === 'column' ||
      key === 'separator'
    ) {
      return true
    } else {
      return false
    }
  }

  static addDocs = (docs, type, data, bim, setData, setBIM) => {
    const updatedAt = Date.now()
    data = cloneDeep(data)
    bim = cloneDeep(bim)

    docs.forEach(doc => {
      if (!this.isBIM(doc.definition)) {
        data.docs[doc.id] = { ...doc, _id: doc.id, updatedAt }
      } else {
        bim.docs[doc.id] = { ...doc, _id: doc.id, updatedAt }
      }
    })

    data.cols = addTitle('data').cols
    for (const key in data.docs) {
      const node = data.docs[key]
      const definition = node.definition
      if (definition) {
        if (!data.cols[definition]) data.cols[definition] = []
        data.cols[definition].push(node)
      }
    }

    bim.cols = addTitle('bim').cols
    for (const key in bim.docs) {
      const node = bim.docs[key]
      const definition = node.definition
      if (definition) {
        if (!bim.cols[definition]) bim.cols[definition] = []
        bim.cols[definition].push(node)
      }
    }

    setData(data)
    setBIM(bim)
  }

  static deleteDocs = (ids, type, data, bim, setData, setBIM) => {
    if (!ids.length) return
    data = cloneDeep(data)
    bim = cloneDeep(bim)
    ids.forEach(id => {
      if (data.docs[id]) delete data.docs[id]
      if (bim.docs[id]) delete bim.docs[id]
    })

    data.cols = addTitle('data').cols
    for (const key in data.docs) {
      const node = data.docs[key]
      const definition = node.definition
      if (definition) {
        if (!data.cols[definition]) data.cols[definition] = []
        data.cols[definition].push(node)
      }
    }

    bim.cols = addTitle('bim').cols
    for (const key in bim.docs) {
      const node = bim.docs[key]
      const definition = node.definition
      if (definition) {
        if (!bim.cols[definition]) bim.cols[definition] = []
        bim.cols[definition].push(node)
      }
    }
    setData(data)
    setBIM(bim)
  }

  static updateDocs = (docs, type, data, bim, setData, setBIM) => {
    if (!docs.length) return
    const updatedAt = Date.now()
    data = cloneDeep(data)
    bim = cloneDeep(bim)

    docs.forEach(doc => {
      if (!this.isBIM(doc.definition)) {
        data.docs[doc.id] = { ...doc, _id: doc.id, updatedAt }
      } else {
        bim.docs[doc.id] = { ...doc, _id: doc.id, updatedAt }
      }
    })

    data.cols = addTitle('data').cols
    for (const key in data.docs) {
      const node = data.docs[key]
      const definition = node.definition
      if (definition) {
        if (!data.cols[definition]) data.cols[definition] = []
        data.cols[definition].push(node)
      }
    }

    bim.cols = addTitle('bim').cols
    for (const key in bim.docs) {
      const node = bim.docs[key]
      const definition = node.definition
      if (definition) {
        if (!bim.cols[definition]) bim.cols[definition] = []
        bim.cols[definition].push(node)
      }
    }
    setData(data)
    setBIM(bim)
  }
}

module.exports = Rxdb
